package com.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dto.AdminLoginDto;
import com.dto.AdminSaveDto;
import com.exceptions.LoginFailedException;
import com.mapper.UserMapper;
import com.po.User;
import com.service.AdminService;
import com.vo.AdminLoginVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @param
 * @return
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AdminServiceImpl extends ServiceImpl<UserMapper, User> implements AdminService {
    private final UserMapper userMapper;


    @Override
    public void registerForAdmin(AdminSaveDto adminSaveDto) {
        User user = new User();
        user.setStatus(1L);
        BeanUtil.copyProperties(adminSaveDto, user);
        user.setCreateTime(LocalDateTime.now());
        user.setIsAdmin(2L);
        this.save(user);
    }

    @Override
    public void setUserStatus(Long userId) {
        //获取用户状态
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>();
        wrapper.eq(User::getId, userId);
        User user = userMapper.selectOne(wrapper);
        // 判断用户是否存在
        if (user != null) {
            Long status = user.getStatus();
            // 切换用户状态
            status = (status == 1) ? 0L : 1L;
            user.setStatus(status); // 更新用户状
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", userId);
            userMapper.update(user, updateWrapper);
        }
    }
}